package sample;
/**
 * Sample Skeleton for 'main.fxml' Controller Class
 */

import java.io.*;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.IndexRange;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


public class Controller {

    private boolean S_pressed = false;
    private boolean Ctrl_pressed = false;
    private File file = FileStatus.f;

    @FXML // fx:id="vBox"
    private VBox vBox;

    @FXML // fx:id="newFileButton"
    private MenuItem newFileButton; // Value injected by FXMLLoader

    @FXML // fx:id="openFile"
    private MenuItem openFile; // Value injected by FXMLLoader

    @FXML // fx:id="saveFile"
    private MenuItem saveFile; // Value injected by FXMLLoader

    @FXML // fx:id="exit"
    private MenuItem exit; // Value injected by FXMLLoader

    @FXML // fx:id="cutText"
    private MenuItem cutText; // Value injected by FXMLLoader

    @FXML // fx:id="copyText"
    private MenuItem copyText; // Value injected by FXMLLoader

    @FXML // fx:id="pasteText"
    private MenuItem pasteText; // Value injected by FXMLLoader

    @FXML // fx:id="textFont"
    private MenuItem textFont; // Value injected by FXMLLoader

    @FXML // fx:id="text"
    private TextArea text; // Value injected by FXMLLoader

    @FXML // fx:id="fontSize
    private TextField fontSize;

    @FXML // fx:id="about"
    private MenuItem about; // Value injected by FXMLLoader


    @FXML
    void aboutClick(ActionEvent event) throws IOException {
        Parent aboutRoot = FXMLLoader.load(getClass().getResource("about.fxml"));
        Stage aboutStage = new Stage();
        aboutStage.setResizable(false);
        aboutStage.setTitle("关于\"notepad\"");
        aboutStage.getIcons().add(new Image("/resources/notepad.png"));
        aboutStage.setScene(new Scene(aboutRoot, 500, 400));
        aboutStage.show();
    }

    @FXML
    void copyClick(ActionEvent event) {
        text.copy();
    }
    @FXML
    void cutClick(ActionEvent event) {
        text.cut();
    }

    @FXML
     void exitClick(ActionEvent event) throws IOException {
        Platform.exit();
    }

    public static boolean checkStrIsNum(String str) {
        Pattern NUMBER_PATTERN = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;
        }
        Matcher isNum = NUMBER_PATTERN.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;

    }

    @FXML
    void setFont(KeyEvent event) {

        String font_size = fontSize.getText();
        if(event.getCode().getName().equals("Enter")&&checkStrIsNum(font_size)){
            text.setStyle("-fx-font-size:"+fontSize.getText());
        }
    }

    @FXML
    void newClick(ActionEvent event) throws IOException {
        //先保存
        saveClick(new ActionEvent());
        //清空
        IndexRange selection = text.getSelection();
        text.deleteText(selection.getStart(), selection.getEnd());
    }

    @FXML
    void onKeyPressed(KeyEvent event) throws IOException {
        switch (event.getCode().getName()) {
            case "Ctrl":
                Ctrl_pressed = true;
                break;
            case "S":
                S_pressed = true;
                break;
        }
        if(Ctrl_pressed&&S_pressed){
            saveClick(new ActionEvent());
        }
    }

    @FXML
    void onKeyReleased(KeyEvent event) {
        switch (event.getCode().getName()) {
            case "Ctrl":
                Ctrl_pressed = false;
                break;
            case "S":
                S_pressed = false;
                break;
        }
    }

    @FXML
    void openClick(ActionEvent event) throws IOException {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("打开文件");
        File f = fileChooser.showOpenDialog(new Stage());
        if(f!=null){
            text.setText(FileStatus.getContent(f));
        }
    }

    @FXML
    void pasteClick(ActionEvent event) {
        text.paste();
    }

    @FXML
    void saveClick(ActionEvent event) throws IOException {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("保存文件");
        fileChooser.setInitialFileName("untitled");
        file = fileChooser.showSaveDialog(new Stage());
        if(file!=null&&!text.getText().isEmpty()){
            FileStatus.setContent(file, text.getText());
        }
    }




    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        //main.fxml
        assert newFileButton != null : "fx:id=\"newFileButton\" was not injected: check your FXML file 'main.fxml'.";
        assert openFile != null : "fx:id=\"openFile\" was not injected: check your FXML file 'main.fxml'.";
        assert saveFile != null : "fx:id=\"saveFile\" was not injected: check your FXML file 'main.fxml'.";
        assert exit != null : "fx:id=\"exit\" was not injected: check your FXML file 'main.fxml'.";
        assert cutText != null : "fx:id=\"cutText\" was not injected: check your FXML file 'main.fxml'.";
        assert copyText != null : "fx:id=\"copyText\" was not injected: check your FXML file 'main.fxml'.";
        assert pasteText != null : "fx:id=\"pasteText\" was not injected: check your FXML file 'main.fxml'.";
        assert textFont != null : "fx:id=\"textFont\" was not injected: check your FXML file 'main.fxml'.";
        assert about != null : "fx:id=\"about\" was not injected: check your FXML file 'main.fxml'.";
        text.setStyle("-fx-font-size:30");
    }
}

